package com.sgb.kjwl.view.ui.adapter.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.swgk.core.util.LogUtil;
import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.RecommendEntity;
import com.sgb.kjwl.utils.LocationUtil;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsInfoActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.TextViewBindingAdapter;
import com.sgb.kjwl.view.widget.RoundImageView;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author CFK
 */
public class SearchGoodsAdapter extends BaseRecycleAdapter<RecommendEntity> {

    private String type = "";
    private static final String TAG = "SearchGoodsAdapter";

    public SearchGoodsAdapter(Context context, List<RecommendEntity> datas) {
        super(context, R.layout.item_search_goods, datas);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void convert(ItemViewHolder holder, final RecommendEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        RelativeLayout root1 = holder.getItem().findViewById(R.id.root_layout1);
        RelativeLayout root2 = holder.getItem().findViewById(R.id.root_layout2);
        TextView companyView = holder.getItem().findViewById(R.id.company_view);
        TextView info = holder.getItem().findViewById(R.id.info);
        TextView titleView = holder.getItem().findViewById(R.id.title_view);
        TextView tvStorageName = holder.getItem().findViewById(R.id.tv_storage_name);
        tvStorageName.setText("仓储:" + entity.getStorageName());
        RoundImageView logoView = holder.getItem().findViewById(R.id.logo_view);
        ImageView imgCx = holder.getItem().findViewById(R.id.img_cx);
        TextView tvPrice = holder.getItem().findViewById(R.id.tv_price);
        TextView tvPriceView = holder.getItem().findViewById(R.id.price_view);
        ImageView ziYing = holder.getItem().findViewById(R.id.ziying_icon);
        ziYing.setVisibility("1".equals(entity.getSelfSupportTag()) ? View.VISIBLE : View.GONE);
        if (LocationUtil.isOpenLocation(context) && LocationUtil.currentLocationIsValid()) {
            holder.getItem().findViewById(R.id.distance_view).setVisibility(View.VISIBLE);
        } else {
            holder.getItem().findViewById(R.id.distance_view).setVisibility(View.GONE);
        }
        if ("material".equals(type)) {
            companyView.setVisibility(View.VISIBLE);
            info.setVisibility(View.GONE);
        } else {
            companyView.setVisibility(View.GONE);
            info.setVisibility(View.VISIBLE);
        }
        if ("recruit".equals(type)) {
            root1.setVisibility(View.GONE);
            root2.setVisibility(View.VISIBLE);
        } else {
            root2.setVisibility(View.GONE);
            root1.setVisibility(View.VISIBLE);
            companyView.setVisibility(View.VISIBLE);
            if (entity.getNegotiable() == 1) {
                tvPrice.setText("面议");
            } else {
                try {
                    info.setVisibility(View.GONE);
                    if (!TextUtils.isEmpty(entity.getPrice())) {
                        tvPriceView.setText("￥" + entity.getPrice() + entity.getPriceUnit());
                    }
                    if (entity.getPromoteStatus() == 2) {
                        imgCx.setVisibility(View.VISIBLE);
                        tvPriceView.setVisibility(View.VISIBLE);
                        if (!TextUtils.isEmpty(entity.getPromotePrice())) {
                            tvPrice.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getPromotePrice()))));
                        }
                        tvPriceView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                    } else {
                        imgCx.setVisibility(View.GONE);
                        tvPriceView.setVisibility(View.GONE);
                        if (!TextUtils.isEmpty(entity.getPrice())) {
                            tvPrice.setText(TextViewBindingAdapter.setMoney(PointUtils.price(Double.parseDouble(entity.getPrice()))));
                        }
                    }

                } catch (NumberFormatException exception) {
                    LogUtil.d(TAG, "price format exception");
                }
            }

        }
        if ("material".equals(type)) {
            companyView.setOnClickListener(v -> {
                if ("material".equals(type)) {
                    if ("0".equals(entity.getCompanyId())) {
                        ShopGoodsInfoActivity.start(context, entity.getId(), "");
                    } else {
                        if (TextUtils.isEmpty(entity.getCompanyNo())) {
                            ToastUtil.show(context, "企业店铺No不能为空");
                            return;
                        }
                        WebsiteInfoActivity.start(context, entity.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
                    }
                }
            });

            titleView.setOnClickListener(v -> {
                if ("material".equals(type)) {
                    ShopGoodsInfoActivity.start(context, entity.getId(), "");
                }
            });

            tvPrice.setOnClickListener(v -> {
                if ("material".equals(type)) {
                    ShopGoodsInfoActivity.start(context, entity.getId(), "");
                }
            });

            logoView.setOnClickListener(v -> {
                if ("material".equals(type)) {
                    ShopGoodsInfoActivity.start(context, entity.getId(), "");
                }
            });
        }
        companyView.setOnClickListener(v -> {
            if (TextUtils.isEmpty(entity.getCompanyNo())) {
                ToastUtil.show(context, "企业店铺No不能为空");
                return;
            }
            WebsiteInfoActivity.start(context, entity.getCompanyNo(), WebsiteInfoActivity.WEBSITE_FOR_COMPANY, 0, 0);
        });
    }

    public void setType(String type) {
        this.type = type;
    }

}
